﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NormReference.DAL;
using System.Data;
using System.Data.Entity;
using System.Collections;
using NormReference.Models;


namespace NormReference.Services
{
    public class FacultyReportWriter
    {
        public static FacultyReportWriter instance;
        private NormReferenceContext db = new NormReferenceContext();

        private FacultyReportWriter()
        {
           
        }

        public static FacultyReportWriter getInstance()
        {
            if(instance == null)
            {
                instance = new FacultyReportWriter();
            }

            return instance;
        }

        public void EmailReport(decimal value, string nsize, FacultyReport facultyReport)
        {
            int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 };
            List<decimal> percentiles = new List<decimal>();
            
            for (int i = 0; i < array.Count(); i++)
            {
                decimal percentile = 0.0m;
                string questionNumber = "Q" + array[i];
                SortedList<decimal, decimal> normReferenceResults = new SortedList<decimal,decimal>();
                string query = "select * from normreferenceresult where questionnumber = '" + questionNumber + "' and nsize = '"
                    + nsize + "'";
                var dblist = db.Database.SqlQuery<NormReferenceResult>(query).ToList();
                
                foreach(NormReferenceResult result in dblist)
                {
                    Logger.getInstance().printToLogFile("Norm Reference Result in Email Report: " + result.ToString());
                    normReferenceResults.Add(result.value, result.probability);
                }
                    
                for(int j = 0; j < normReferenceResults.Count; j++)
                {
                    decimal probability = normReferenceResults.Values.ElementAt(j);
                    percentile += probability;
                    Logger.getInstance().printToLogFile("Probability in Email Report: " + probability);
                    Logger.getInstance().printToLogFile("Current Percentile in Email Report: " + percentile);
                    if(normReferenceResults.Keys.ElementAt(j) == value)
                    {
                        percentiles.Add(percentile);
                        break;
                    }
                }
            }    
       
            string subject = "Norm Reference Results";
            string body = "";
            int k = 0;

            foreach(decimal rank in percentiles)
            {
                body += "Question " + array[k] + " Value: " + value + " ";
                body+= rank.ToString() + " \n";
                k++;
            }

            Logger.getInstance().printToLogFile("Emailing Report!");
            Faculty faculty = db.faculty.Find(facultyReport.facultyID);
            EmailWriter.getInstance().SendMessage(faculty.email, subject, body);
            Logger.getInstance().printToLogFile("Finished Emailing Report!");

        }
    }
}